package com.threeonemeeting.activity.ext;

import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.threeonemeeting.Const;
import com.threeonemeeting.adapter.MeetingInfoListAdapter;
import com.threeonemeeting.list.FullMeetingInfoList;
import com.threeonemeeting.model.FullMeetingInfo;
import com.threeonemeeting.model.MeetingInfo;

/**
 * @desc 如果页面的主要内容是列表，并且需要支持分页，并且支持下拉刷新， 那么可以从 PageListActivity继承
 * @author Administrator
 * 
 */
public class MeetingPageListActivity extends PageListActivity {

	protected MeetingInfoListAdapter mAdapter;

	public void setContentView(int layoutResId, String strTitle) {
		super.setContentView(layoutResId, strTitle);

		// ListView
		mAdapter = new MeetingInfoListAdapter(this);
		meetingListView.setAdapter(mAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long id) {

		// 因为支持拖放，有HeaderView，因此实际的1对应的是Adapter中的0
		int nIndex = position - 1;
		if (nIndex < 0)
			return;

		if (nIndex > mAdapter.getCount() - 1)
			return;

		MeetingInfo mInfo = mAdapter.getItem(nIndex);
		if (mInfo == null)
			return;

		int nId = mInfo.nId;

		// 跳转到会议详情页面
		Bundle b = new Bundle();
		b.putInt("id", nId);
		startActivity("MeetingDetailActivity", b);
	}

	public void onSuccessCallBack(Map<String, Object> mapData) {
		meetingListView.stopLoadingMore();

		FullMeetingInfoList listMeetings = (FullMeetingInfoList) mapData
				.get(Const.Keys.KEY_LIST);
		if (listMeetings == null)
			return;

		Integer nPage = (Integer) mapData.get("page");
		if (nPage == null)
			nPage = 1;

		if (nPage == 1) {
			mAdapter.clear();
		}

		if (listMeetings.size() == 0 || listMeetings.size() < Const.PAGE_NUM) {
			meetingListView.setLastPage(nPage);
		}

		for (int i = 0; i < listMeetings.size(); i++) {
			FullMeetingInfo fullInfo = listMeetings.get(i);
			if (fullInfo == null)
				continue;

			MeetingInfo info = new MeetingInfo(fullInfo);
			mAdapter.add(info);
		}

		showListLayout(listMeetings.size());
		showLoadingMoreLayout(false);

		mAdapter.notifyDataSetChanged();
		meetingListView.onRefreshComplete();
		meetingListView.onLoadMoreComplete();
	}
}
